<template>
  <div class="app-container" ref="appcontainer">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleAddTmp"
          >添加模板</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :height="tableHeight"
      :data="list"
      @close="handleClose"
    >
      <el-table-column label="id" align="center" width="55" prop="id" />
      <el-table-column label="标题" align="center" width="200" prop="title" />
      <el-table-column
        label="内容"
        align="center"
        prop="content"
      ></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleEdit(scope.row)"
            >修改</el-button
          >
          <el-button size="mini" type="text" @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="500px"
      :before-close="handleClose"
    >
      <el-form
        :model="form"
        ref="form"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="标题" prop="title">
          <el-input
            v-model.number="form.title"
            autocomplete="off"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input
            type="textarea"
            v-model.number="form.content"
            autocomplete="off"
            style="width: 300px"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfimSmsOrEdit"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getList, addSms, editSms, delSms } from "@/api/sms";
export default {
  data() {
    return {
      tableHeight: 0,
      title: "添加短信模板",
      dialogVisible: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      form: {
        title: "",
        content: "",
      },
      isEdit: false,
      loading: false,
      list: [],
    };
  },
  methods: {
    calHeight() {
      this.$nextTick(() => {
        const rect = this.$refs.appcontainer.getBoundingClientRect();
        this.tableHeight = rect.height - 80;
      });
    },
    getList() {
      this.loading = true;
      getList(this.queryParams)
        .then((res) => {
          console.log(res);
          this.list = res.rows;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleEdit(row) {
      this.isEdit = true;
      this.form = {
        id: row.id,
        title: row.title,
        content: row.content,
      };
      this.dialogVisible = true;
    },
    handleDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delSms(id).then(() => {
          this.list = [];
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },
    handleAddTmp() {
      this.dialogVisible = true;
    },
    handleConfimSmsOrEdit() {
      if (this.isEdit) {
        editSms(this.form).then(() => {
          this.msgSuccess("编辑成功");
          this.handleClose();
          this.dialogVisible = false;
          this.list = [];
          this.getList();
        });
      } else {
        addSms(this.form).then((res) => {
          this.msgSuccess("添加成功");
          this.handleClose();
          this.dialogVisible = false;
          this.list = [];
          this.getList();
        });
      }
    },
    handleClose() {
      this.form = {};
      this.isEdit = false;
    },
  },
  mounted() {
    this.calHeight();
    this.getList();
  },
};
</script>